import {
  ADD_CART,
  CHANGE_HEADER_DELETE,
  CART_DETAIL_DELETE,
  CART_EDIT,
  CART_PAY,
  ORDER_LISTED,
  ADD_ADDRESS,
  EDIT_ADDRESS,
  DELETE_ADDRESS,
} from "./mutations-types";

export default {
  [ADD_CART](state, value) {
    state.cartList = value;
  },
  [CHANGE_HEADER_DELETE](state) {
    state.isDelete = !state.isDelete;
  },
  [CART_DETAIL_DELETE](state, value) {
    state.cartList = value;
  },
  [CART_EDIT](state, value) {
    if (value) {
      state.edit = true;
    } else {
      state.edit = !state.edit;
    }
  },
  [CART_PAY](state, value) {
    state.orderList = value;
  },
  [ORDER_LISTED](state) {
    state.orderListEnd = state.orderListEnd.concat(state.orderList);
  },
  [ADD_ADDRESS](state, value) {
    state.userAddress.map((item) => {
      if (value.isDefault) {
        item.isDefault = false;
      }
    });
    state.userAddress.push(value);
  },
  [EDIT_ADDRESS](state, value) {
    state.userAddress = state.userAddress.map((item) => {
      if (value.isDefault) {
        item.isDefault = false;
      }

      /* 
            if (item.id === value.id) {
                return value;
            } else {
                return item;
            }
            */

      return item.id === value.id ? value : item;
    });
  },
  [DELETE_ADDRESS](state, value) {
    state.userAddress = state.userAddress.filter((item) => {
      return !(item.id === value.id);
    });
    if (value.isDefault) {
      state.userAddress[0].isDefault = true;
    }
  },
};
